package com.eascs.admin.o2o.notify.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.account.account.AccountEntity;
import com.eascs.admin.common.service.IO2oLoanapplyDubboService;
import com.eascs.admin.o2o.back.entity.O2oBackEntity;
import com.eascs.admin.o2o.back.service.O2oBackService;
import com.eascs.admin.o2o.backFees.entity.O2oBackFeesEntity;
import com.eascs.admin.o2o.backFees.service.O2oBackFeesService;
import com.eascs.admin.o2o.backfee.entity.O2OComBackFee;
import com.eascs.admin.o2o.backfee.service.O2OComBankFeeService;
import com.eascs.admin.o2o.badloan.entity.O2oBadRecordEntity;
import com.eascs.admin.o2o.badloan.service.O2oBadLoanService;
import com.eascs.admin.o2o.cacle.entity.O2oCacleFlowEntity;
import com.eascs.admin.o2o.cacle.service.O2oCacleFlowServiceImpl;
import com.eascs.admin.o2o.credit.entity.CreditReport;
import com.eascs.admin.o2o.credit.service.CreditReportService;
import com.eascs.admin.o2o.creditarchive.service.CreditArchiveService;
import com.eascs.admin.o2o.enterpriseZX.entity.O2oEnterpriseZxEntity;
import com.eascs.admin.o2o.enterpriseZX.service.O2oEnterpriseZxService;
import com.eascs.admin.o2o.generation.entity.O2oGenerationEntity;
import com.eascs.admin.o2o.generation.service.O2oGenerationService;
import com.eascs.admin.o2o.internal.entity.AuditComments;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpandDetail;
import com.eascs.admin.o2o.internal.service.AuditCommentService;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandDetailService;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.BCRecommendationEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.BCRecommendationService;
import com.eascs.admin.o2o.loan.service.O2oChargeSituationService;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loan.utils.BankRate;
import com.eascs.admin.o2o.loanAccount.entity.O2oCollectFee;
import com.eascs.admin.o2o.loanAccount.service.IO2oCollectFeesService;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oLoadUserZxServiceImpl;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.settle.entity.O2oSettleEntity;
import com.eascs.admin.o2o.settle.service.O2oSettleService;
import com.eascs.admin.o2o.settle.vo.O2oSettleVo;
import com.eascs.admin.o2o.specialInterview.entity.O2OSpecialInterview;
import com.eascs.admin.o2o.specialInterview.service.O2OSpecialInterviewService;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.afterloan.entity.AdvancedPayment;
import com.eascs.afterloan.service.AdvanceSettleService;
import com.eascs.afterloancheck.dao.LoanCheckDao;
import com.eascs.afterloancheck.entity.CheckLoanEntity;
import com.eascs.afterloancheck.service.LoanCheckServicce;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.archive.dao.ArchiveRecordDao;
import com.eascs.archive.entity.ArchiveRecordEntity;
import com.eascs.archive.entity.ArchiveRecordRepeatEntity;
import com.eascs.archive.entity.CreditArchiveRecordEntity;
import com.eascs.archive.service.ArchiveRecordService;
import com.eascs.archive.untils.ArchiveConstants;
import com.eascs.brand.dao.O2oPartnerDao;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.service.CarInfoService;
import com.eascs.crm.nsEditDetail.service.HouseInfoService;
import com.eascs.crm.nsEditDetail.service.OtherInfoService;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.demand.service.TicketMainApplyService;
import com.eascs.file.FileService;
import com.eascs.file.FileTypeConstants;
import com.eascs.loan.dao.O2oLoanDao;
import com.eascs.loan.entity.O2oBankLoanData;
import com.eascs.loan.entity.O2oPaymentsPlan;
import com.eascs.loan.service.O2oLoanService;
import com.eascs.loan.service.O2oPaymentsService;
import com.eascs.pay.entity.O2oPayEntity;
import com.eascs.pay.service.impl.O2oPayServiceImpl;
import com.eascs.paymentdetail.entity.O2oPaymentDetailEntity;
import com.eascs.paymentdetail.service.O2oPaymentDetailService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.WorkFlowConstants;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.service.wf.WorkFlowNodeTransferCheck;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNodeCheckParam;
import com.eascs.workflow.service.LoanapplyExpandUpdateService;
import com.eascs.workflow.util.TranferResult;


@Service
public class WorkFlowNodeTransferCheckImpl implements WorkFlowNodeTransferCheck {
	/**
	 * 节点流转check
	 * 
	 * @param nodeCheckParam
	 * @return
	 */
	private static Logger log = LoggerFactory.getLogger(WorkFlowNodeTransferCheckImpl.class);
	@Inject
	TicketMainApplyService ticketMainApplyService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Autowired
	private O2oDataListService o2oDataListService;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Autowired
	private O2oLoanService loanService;
	@Autowired
	O2OComBankFeeService o2oComBankFeeService;
	@Autowired
	private O2oPaymentsService o2oPaymentsService;
	@Autowired
	O2oLoanDao loanDao;
	@Value("${o2o.admin.environment}")
	private String environmentConfig;
	@Autowired
	private FileService fileService;
	@Autowired
	O2oPayServiceImpl o2oPayservice;
	@Autowired
	private CreditReportService creditReportService;
	@Autowired
	private O2oPaymentDetailService o2oPaymentDetailService;
	@Autowired
	private BCRecommendationService bcRecommendationService;
	@Autowired
	private LoanapplyExpandUpdateService loanapplyExpandUpdateService;
	@Autowired
	private AuditCommentService auditCommentService;
	@Autowired
	private ArchiveRecordService archiveRecordService;
	@Autowired
	private O2oBadLoanService service;
	@Autowired
	O2oPartnerDao o2oPartnerDao;
	@Autowired
	AdvanceSettleService advanceSettleService;
	@Autowired
	LoanCheckServicce loanCheckServicce;
	@Autowired
	LoanCheckDao loanCheckDao;
	@Autowired
	private O2oBackService o2oBackService;
	
	@Autowired
	O2oCacleFlowServiceImpl  o2oCacleFlowServiceImpl;
 
	@Autowired
	O2OSpecialInterviewService o2OSpecialInterviewService;
 
	@Autowired
	private LoanapplyExpandDetailService loanapplyExpandDetailService;
	
	@Autowired
	private CreditArchiveService creditArchiveService;
	@Autowired
	private ArchiveRecordDao archiveRecordDao;
	
	@Autowired
	HouseInfoService houseInfoService;
	@Autowired
	CarInfoService carInfoService;
	@Autowired
	OtherInfoService otherInfoService;
	
	@Autowired
	O2oZxServiceImpl  o2oZxServiceImpl;
	@Autowired
	O2oLoadUserZxServiceImpl o2oLoadUserZxServiceImpl;
	@Autowired
	O2oEnterpriseZxService o2oEnterpriseZxService;
	@Autowired
	O2oBackFeesService o2oBackFeesService;
	@Autowired
	IO2oCollectFeesService o2oCollectFeesService;
	@Autowired
	O2oChargeSituationService o2oChargeSituationService;
	@Autowired
	IO2oLoanapplyDubboService loanapplyDubboService;
	
	public ModelResult<Boolean> check(WFNodeCheckParam nodeCheckParam) {
		log.info("接受来自工作流的check开始,ReferId：[{}]", nodeCheckParam.getReferId());
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		log.info("接受来自工作流APPnodeCheckParam.getLoginUser()不为空 UserHolder账户：[{}]", nodeCheckParam.getLoginUser());
		User user=UserHolder.getUser();
		try {
		if(UserHolder.getUser()==null){
			UserHolder.setUser(nodeCheckParam.getLoginUser());
			log.info("设置UserHolder成功：{}",UserHolder.getUser().getCname());
		}
		String urlFromId = "";
		String url = nodeCheckParam.getReferUrl();
		
		if (nodeCheckParam.getProcessType().equals("PROCESS_CACLE") ) { 
			// 手动作废
			return checkNewCacle(result, nodeCheckParam.getReferId(), urlFromId, nodeCheckParam);
		}
		
		
		if (nodeCheckParam.getProcessType().equals("ACCOUNT_REFUND") ) {
			// 个人账户退款流程
			return checkTX(result, nodeCheckParam.getReferId(), urlFromId, nodeCheckParam);
		}
		
		if (nodeCheckParam.getProcessType().equals("ACCOUNT_REMITTANCE") ) {
			// 个人账户打款流程
			return checkCollectFees(result, nodeCheckParam.getReferId(), urlFromId, nodeCheckParam);
		}
		
		if (nodeCheckParam.getProcessType().equals("E_CREDIT") ) {
			// 企业征信流程
			return checkQYZX(result, nodeCheckParam.getReferId(), urlFromId, nodeCheckParam);
		}
		
		if (nodeCheckParam.getProcessType().equals(StatusContexts.WF_TYPE_CREDIT_INQUIRY) ) {
			// 新征信流程
			return checkNewZX(result, nodeCheckParam.getReferId(), urlFromId, nodeCheckParam);
		}
		
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_PAY) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_PAY_FQ)) {// 付款流程
			// 付款流程
			return checkPay(result, nodeCheckParam.getReferId(), urlFromId, nodeCheckParam);
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_BACK_MONEY)) {// 回款流程
			return checkBadLoan(result, nodeCheckParam);
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_DCJQ)) {// 代偿结清流程
			return checkSettle(result, nodeCheckParam);
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_DHK)) {// 代还款流程
			return checkGeneration(result, nodeCheckParam);
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_DC_FED_BACK)) {// 代偿分期流程
			return checkBack(result, nodeCheckParam.getReferId(), nodeCheckParam);
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_BORROW_ARCHIVE)) {// 归档借阅流程
			return checkGDBorrow(result, nodeCheckParam.getReferId(), nodeCheckParam);
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_BC_ARCHIVE)) {// 补充归档流程
			return checkGDRepeat(result, nodeCheckParam.getReferId(), nodeCheckParam);
		}
		
		if(nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_CREDIT_ARCHIVE)){//征信归档流程check
			//征信归档流程
			CreditArchiveRecordEntity creditArchiveRecordEntity=creditArchiveService.getCreditArchiveRecordById(nodeCheckParam.getReferId());
			return checkCreditArchive(result,creditArchiveRecordEntity,nodeCheckParam);
		}
		
		
		
		
		if (StringUtil.isNull(url)) {
			log.info("工作流配置动态表单URL为空");
			result.withModel(true);
			return result;
		}
		
		String urlA[] = url.split("/");
		urlFromId = urlA[urlA.length - 1];
		log.info("工作流配置动态表单URL:[{}],ReferId[{}]", urlFromId, nodeCheckParam.getReferId());
		O2oLoanapplyEntity loanEntity = o2oLoanapplyService.findById(nodeCheckParam.getReferId());
		
		AdvancedPayment advancedPayment = advanceSettleService.queryEntity(nodeCheckParam.getReferId(), null);
		
		if (loanEntity == null) {
			log.info("根据ReferId:[{}]查询贷款主表为空", nodeCheckParam.getReferId());
			result.withError(WorkFlowConstants.ENTITY_NULL, "实体不存在，请检查ReferId是否存在");
			return result;
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_CREDIT) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_CREDI_FQ) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_CREDI_BP) || nodeCheckParam.getProcessType().equals("PROCESS_ACCESS")) {
			// 征信流程
			return checkCredit(result, loanEntity, urlFromId);
		} else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT_FQ) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT_BP) ) {// 内审
			// 内审流程
			return checkNS(result, loanEntity, urlFromId, nodeCheckParam);
		} else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_DEPOSIT) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_DEPOSIT_FQ)) {// 保证金
			// 收取保证金、管理费流程
			return checkDeposit(result, loanEntity, urlFromId, nodeCheckParam);
		} else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_BANK_REPORT) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_BANK_REPORT_FQ)) {// 银行申报
			// 银行送审流程
			return checkBankSumbit(result, loanEntity, urlFromId, nodeCheckParam);
		} else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_ARCHIVE)) {// 归档
			//归档流程 NEW
			return checkArchiveBackFile(result, loanEntity, url, nodeCheckParam);
		}else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_REFUND_MGR_FEE_FQ) || nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_REFUND_MGR_FEE)) {// 退费(保证金、管理费)流程
			// 退费(保证金、管理费)流程
			return checkRetreat(result, loanEntity, urlFromId, nodeCheckParam);
		}else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_PARTNERS_AUDIT_BP)) {// 合作方申请流程
			// 合作方申请流程
			return checkPartnerBackFile(result, loanEntity, urlFromId, nodeCheckParam);
		}else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_REGULAR_CHECK_LOAN_AFTER)) {// 定期贷后检查申请流程
			// 定期贷后检查申请流程
			CheckLoanEntity checkEntity = loanCheckServicce.queryEntityByReferId(nodeCheckParam.getReferId());
			return checkLoanCk(result, checkEntity, urlFromId, nodeCheckParam);
		}else if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_TQ_PAYMENT)){
			//提前还款结清
			return checkTQHK(result, advancedPayment, urlFromId, nodeCheckParam);
		}else if(nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_SPEC_BANK_REPORT)){  // 特殊面签申报流程check
			// 特殊面签申报流程
			return checkTSMQSB(result, urlFromId, nodeCheckParam);
		}
		log.info("接受来自工作流的check结束,ReferId：[{}]", nodeCheckParam.getReferId());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("工作流Check异常,WorkFlowNodeTransferCheckImpl:{}", e.getMessage());
		}finally {
			UserHolder.setUser(user);
		}
		result.setModel(true);
		return result;
	}
	
	/**
	 * 贷后定期检查申请-信贷经理审核内容check
	 * @param result
	 * @param checkEntity
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkLoanCk(ModelResult<Boolean> result, CheckLoanEntity checkEntity, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("定期贷后检查申请流程check开始,贷款loanapplyNo:[{}],", checkEntity.getLoanApplyNo());
		if (urlFromId.equals(WorkFlowConstants.AFTER_LAON_CHECK_URL)) { // 信贷经理审核
			log.info("信贷经理审核表单URL:[{}]", WorkFlowConstants.AFTER_LAON_CHECK_URL);
			if (checkEntity.getLoanNumber() == null) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "贷款次数不能空");
				return result;
			} else if (!StringUtil.isNotBlank(checkEntity.getRevisiterName())) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "回访人不能空");
				return result;
			} else if (checkEntity.getStockMoney() == null) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "贷前存货金额不能空");
				return result;
			} else if (checkEntity.getBalancesReceivable() == null) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "应收账款余额不能空");
				return result;
			} else if (checkEntity.getMonthlySale() == null) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "月均销售额不能空");
				return result;
			} else if (checkEntity.getMonthlyPurchases() == null) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "向核心企业月采购额不能空");
				return result;
			} else if (checkEntity.getNewDebt() == null) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "新增负债额不能空");
				return result;
			} else if (checkEntity.getRevisitDate() == null) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "回访日期不能空");
				return result;
			} else if (!StringUtil.isNotBlank(checkEntity.getIsEnvironmentChanges())) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "经营环境变化不能空");
				return result;				
			} else if (!StringUtil.isNotBlank(checkEntity.getIsPlaceChanges())) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "经营场地变化不能空");
				return result;						
			} else if (!StringUtil.isNotBlank(checkEntity.getIsStaffChanges())) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "经营人员变化不能空");
				return result;					
			} else if (!StringUtil.isNotBlank(checkEntity.getIsOutTime())) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "存货超出有效期不能空");
				return result;						
			} else if (!StringUtil.isNotBlank(checkEntity.getIsFamilyChange())) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "家庭发生变化不能空");
				return result;						
			} else if (!StringUtil.isNotBlank(checkEntity.getCheckDes())) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "贷后检查内容描述不能空");
				return result;						
			} else if (!loanCheckDao.checkCodeAttachment(checkEntity.getId(), FileTypeConstants.LOAN_CHECK_TYPE_RPMENT)) {
				result.withError(WorkFlowConstants.LOAN_AFTER_CHECK_FAILL, "请上传贷后定期检查资料");
				return result;
			}
			result.setModel(true);
		}
		return result;
	}

	/**
	 * 征信流程Check
	 * 
	 * @param result
	 * @param loanEntity
	 * @param urlFromId
	 * @return
	 */
	private ModelResult<Boolean> checkCredit(ModelResult<Boolean> result, O2oLoanapplyEntity loanEntity, String urlFromId) {
		log.info("征信流程check开始,贷款loanapplyNo:[{}]", loanEntity.getLoanapplyNo());
		log.info("Check征信URL:[{}]", urlFromId);
		if (urlFromId.equals(WorkFlowConstants.ZX_FROM_ID_YHSSZY_URL)) { // 总部征信送审节点
			log.info("Check总部征信送审表单URL：[{}]", WorkFlowConstants.ZX_FROM_ID_YHSSZY_URL);
			if (!StringUtil.isNotBlank(loanEntity.getBankType())) {
				result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "送审银行不能为空");
				return result;
			}
//			O2oLoanUserEntity loanUser = o2oLoanUserService.findSpouseId(loanEntity.getLoanId(), loanEntity.getLoanapplyNo());
//			String mater = null;
//			if (loanUser != null) {
//				mater = loanUser.getLoanId();
//			}
//			if (!o2oDataListService.checkZxAttachment(loanEntity.getLoanapplyNo(), mater)) { // 查找附件信息
//				if (!loanEntity.getBankType().equals(BankRate.BANK_BC)) {
//					if (mater != null) {
//						result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "上传附件未齐全");
//						return result;
//					} else {
//						result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "请上传附件");
//						return result;
//					}
//				} else {
//					result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "未查到相关征信报告，请等待中行返回征信报告。如有问题请联系管理员。");
//					return result;
//				}
//			}
//			if (BankRate.BANK_BC.equals(loanEntity.getBankType())) {
//				O2oLoanUserEntity mainloanUser = o2oLoanUserService.findById(loanEntity.getLoanId(), loanEntity.getLoanapplyNo());
//				if (!"01B3".equals(mainloanUser.getBankStatus())) {
//					result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "主审请人银行状态为已同意贷前调查,才能通过。如有问题请联系管理员。");
//					return result;
//				}
//			}
		} else if (urlFromId.equals(WorkFlowConstants.ZX_FROM_ID_ZXQR_URL)) { // 总部征信确认
			log.info("Check总部征信确认URL:[{}]", WorkFlowConstants.ZX_FROM_ID_ZXQR_URL);
			O2oLoanUserEntity loanUser = o2oLoanUserService.findSpouseId(loanEntity.getLoanId(), loanEntity.getLoanapplyNo());
			log.info("查询用户配偶信息:{}", loanUser);
			String mater = null;
			if (loanUser != null) {
				mater = loanUser.getLoanId();
			}
			log.info("查询用户配偶信息LoanId:{}", mater);
			if (!o2oDataListService.checkZxAttachment(loanEntity.getLoanapplyNo(), mater)) { // 查找附件信息
				if (!loanEntity.getBankType().equals(BankRate.BANK_BC)) {
					if (mater != null) {
						result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "上传附件未齐全");
						return result;
					} else {
						result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "请上传附件");
						return result;
					}
				} else {
					result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "未查到相关征信报告，请等待中行返回征信报告。如有问题请联系管理员。");
					return result;
				}
			}
			if (BankRate.BANK_BC.equals(loanEntity.getBankType())) {
				O2oLoanUserEntity mainloanUser = o2oLoanUserService.findById(loanEntity.getLoanId(), loanEntity.getLoanapplyNo());
				if (!"01B3".equals(mainloanUser.getBankStatus())) {
					result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "主审请人银行状态为已同意贷前调查,才能通过。如有问题请联系管理员。");
					return result;
				}
			}
//			O2oSDEntity sdEntity = o2oSdService.getById(loanEntity.getLoanapplyNo());
//			if (sdEntity == null) {
//				result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "请先保存SD数据表单");
//				return result;
//			}
			/*if (loanEntity.getEaCooperationDate() == null) {
				result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "与怡亚通（前身）合作开始时间不能空.");
				return result;
			}
			if (loanEntity.getCustomerSalesamount() == null) {
				result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "客户年销售额不能为空");
				return result;
			}*/
		} else if (urlFromId.equals(WorkFlowConstants.ZX_FROM_ID_ZX_SSYH_URL)) {// 总部风险经理、总部风险总监节点
			log.info("Check总部风险经理、总部风险总监表单:[{}]", WorkFlowConstants.ZX_FROM_ID_PTCW_URL);
			CreditReport report = creditReportService.queryLoanapplyNo(loanEntity.getLoanapplyNo(), String.valueOf(StatusContexts.O2O_ZERO));
			if (report == null) {
				result.withError(WorkFlowConstants.ZX_TYPE_FAILL, "请先编辑征信情况");
				return result;
			}
		}
		log.info("征信流程check成功,贷款loanapplyNo:[{}]", loanEntity.getLoanapplyNo());
		result.withModel(true);
		return result;
	}

	/**
	 * Check内审流程
	 * 
	 * @param result
	 * @param loanEntity
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkNS(ModelResult<Boolean> result, O2oLoanapplyEntity loanEntity, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("内审流程check开始,贷款loanapplyNo:[{}],工作流节点NodeCode:[{}]", loanEntity.getLoanapplyNo(),nodeCheckParam.getNodeCode());
		if (nodeCheckParam.getNodeCode() != null) {
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.NS_ZBCS) || nodeCheckParam.getNodeCode().equals(WorkFlowConstants.NS_ZBFS)) {
//				if (loanEntity.getBankType().equals(WorkFlowConstants.BANK_TYPE_BC)) { // 如果送审银行是中行
//					if (loanEntity.getCreditline() != null) {
//						log.info("内审时银行预审批额度:[{}]", loanEntity.getCreditline());
//					} else {
//						log.info("中行未返回预审批额度请等待中行返回预审批额度，如有疑问请联系管理员");
//						result.withError(WorkFlowConstants.NS_TYPE_FAILL, "中行未返回预审批额度请等待中行返回预审批额度，如有疑问请联系管理员。");
//						return result;
//					}
//				}
			}/*else if((WorkFlowConstants.NS_FKJL).equals(nodeCheckParam.getNodeCode())){
				//房产
				boolean flag = houseInfoService.checkHouse(loanEntity.getLoanapplyNo());
				if(flag){
					log.info("省区风控经理房产估值未填");
					result.withError(WorkFlowConstants.NS_TYPE_FAILL, "省区风控经理房产估值未填");
					return result;
				}
				//车产
				boolean flag1 = carInfoService.checkCar(loanEntity.getLoanapplyNo());
				if(flag1){
					log.info("省区风控经理车产估值未填");
					result.withError(WorkFlowConstants.NS_TYPE_FAILL, "省区风控经理车产估值未填");
					return result;
				}
				//其他资产
				boolean flag2 = otherInfoService.checkOtherAssets(loanEntity.getLoanapplyNo());
				if(flag2){
					log.info("省区风控经理其他资产估值未填");
					result.withError(WorkFlowConstants.NS_TYPE_FAILL, "省区风控经理其他资产估值未填");
					return result;
				}
			}*/
			/*else if("NS_XDJL".equals(nodeCheckParam.getNodeCode())){
				log.info("信贷经理节点,check回购担保方code", loanEntity.getBuybackCode());
				if(!StringUtil.isNotBlank(loanEntity.getBuybackCode()) || !StringUtil.isNotBlank(loanEntity.getBuybackName()) || !StringUtil.isNotBlank(loanEntity.getBuybackId())){
					result.withError(WorkFlowConstants.NS_TYPE_FAILL, "回购担保方不能为空");
					return result;	
				}
			}*/
		}
		if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT)||nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT_FQ)||nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT_BP)) {
			List<AuditComments> auditCommentsList = auditCommentService.queryUserNo(loanEntity.getLoanapplyNo(), nodeCheckParam.getAuditorId());
			log.info("内审流程校验每个审核人是否有保存,贷款ID：loanapplyNo[{}],当前审核人:AuditorId[{}]", loanEntity.getLoanapplyNo(), nodeCheckParam.getAuditorId());
			//判断环境是否为正式
			if(this.environmentConfig.equals("prod")){
				if (null == auditCommentsList || auditCommentsList.isEmpty()) {
					result.withError(WorkFlowConstants.NS_TYPE_FAILL, "请先保存表单");
					return result;
			}
			}
		}
		if (urlFromId.equals(WorkFlowConstants.NS_FROM_ID_ALL_URL)) {// 内审动态表单
			log.info("Check内审动态表单:[{}]", WorkFlowConstants.NS_FROM_ID_ALL_URL);
			if (!StringUtil.isNotBlank(String.valueOf(loanEntity.getLoanTerm()))) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "货款期限不能空");
				return result;
			}
			if (!StringUtil.isNotBlank(loanEntity.getRepayMentway())) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "还款方式不能空");
				return result;
			}
			if (loanEntity.getLoanrate() == null) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "贷款利率不能空");
				return result;
			}
			if (loanEntity.getManagefeerate() == null) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "管理费率不能空");
				return result;
			}
			if (loanEntity.getDepositrate() == null) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "保证金费率不能空");
				return result;
			}
			LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loanEntity.getLoanapplyNo());
			if (loanapplyExpand == null) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "请先保存表单信息");
				return result;
			}
			if (loanapplyExpand.getRisk() == null) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "保证金费率不能空");
				return result;
			}
			if (loanEntity.getEaMount() == null) {
				result.withError(WorkFlowConstants.NS_TYPE_FAILL, "ea批复额度不能为空");
				return result;
			}
			if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT_FQ)) {// 分期贷和接力贷
				if (StatusContexts.LOAN_TYPE_EA.equals(loanEntity.getLoanType()) || StatusContexts.LOAN_TYPE_FQ.equals(loanEntity.getLoanType())) {
					if (!o2oDataListService.checkCodeAttachment(loanEntity.getLoanapplyNo(), FileTypeConstants.DALIST_TYPE_CONTRACT)) {
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请上传面签材料");
						return result;
					}
				}
			}
		} else if (urlFromId.equals(WorkFlowConstants.NS_FROM_ID_NS_URL)) { // 分期贷和接力贷内审
			log.info("分期贷和接力贷内审URL:[{}]", WorkFlowConstants.NS_FROM_ID_NS_URL);
			if (nodeCheckParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_INAUDIT_FQ)) {
				if (StatusContexts.LOAN_TYPE_EA.equals(loanEntity.getLoanType()) || StatusContexts.LOAN_TYPE_FQ.equals(loanEntity.getLoanType())) {
					if (StringUtil.isEmpty(loanEntity.getIsCheckMqCl())) {
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先选择合同面签视频、合同面签照片、个人手持征信授权书及身份证照片三类电子文档是否已上传");
						return result;
					}
					if (loanEntity.getIsCheckMqCl().equals(StatusContexts.O2O_NO)) {
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "合同面签视频、合同面签照片、个人手持征信授权书及身份证照片三类电子文档未上传齐全，请确认上传齐备后在提交。");
						return result;
					}
				}
			}
		}
		log.info("内审流程check成功,贷款loanapplyNo:[{}]", loanEntity.getLoanapplyNo());
		result.withModel(true);
		return result;
	}

	/**
	 * 收取保证金、管理费流程
	 * 
	 * @param result
	 * @param loanEntity
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkDeposit(ModelResult<Boolean> result, O2oLoanapplyEntity loanEntity, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("收取保证金、管理费流程Check开始,贷款loanapplyNo：[{}]", loanEntity.getLoanapplyNo());
		LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loanEntity.getLoanapplyNo());
		LoanapplyExpandDetail loanapplyExpandDetail = loanapplyExpandDetailService.queryDetail(loanapplyExpand.getId());
		if (urlFromId.equals(WorkFlowConstants.DEPOSIT_FROM_ID_PTCW_URL)) { // 收取保证金平台财务
			log.info("收取保证金平台财务URL:[{}]", WorkFlowConstants.DEPOSIT_FROM_ID_PTCW_URL);
			if (loanapplyExpandDetail != null) {
				if (loanapplyExpandDetail.getPayName() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "付款人不能空");
					return result;
				}
				if (loanapplyExpandDetail.getSubmitDate() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "要求付款时间不能空");
					return result;
				}
				if (loanapplyExpandDetail.getTransferNum() != null) {
					loanapplyExpand.setRedDeposite(loanapplyExpandDetail.getRedDeposite());
					loanapplyExpand.setRedManger(loanapplyExpandDetail.getRedManger());
					loanapplyExpand.setRedRisk(loanapplyExpandDetail.getRedRisk());
					loanapplyExpand.setReceiveDeposit(loanapplyExpandDetail.getReceiveDeposit());
					loanapplyExpand.setPayDate(loanapplyExpandDetail.getPayDate());
					loanapplyExpand.setPayName(loanapplyExpandDetail.getPayName());
					loanapplyExpand.setPaidDeposite(loanapplyExpandDetail.getPaidDeposite());
					loanapplyExpand.setPaidManger(loanapplyExpandDetail.getPaidManger());
					loanapplyExpand.setPaidRisk(loanapplyExpandDetail.getPaidRisk());
					loanapplyExpand.setPayRemark(loanapplyExpandDetail.getPayRemark());
					loanapplyExpand.setPayDate(loanapplyExpandDetail.getPayDate());
					loanapplyExpand.setSubmitDate(loanapplyExpandDetail.getSubmitDate());
					if(loanapplyExpandDetail.getTransferNum().contains(",")){
						String str[]=loanapplyExpandDetail.getTransferNum().split(",");
						for (int i = 0; i < str.length; i++){
							loanapplyExpand.setTransferNum(str[i]);
							if(loanapplyExpandDetail.getTrandepositNo().contains(",")){
								String tranStr[]=loanapplyExpandDetail.getTrandepositNo().split(",");
								for (int j = i; j < tranStr.length;){
									loanapplyExpand.setTransferDeposit(new BigDecimal(tranStr[j]));
									log.info("划拨单号:[{}],划拨金额：[{}]",str[i],tranStr[j]);
									TranferResult tfr = loanapplyExpandUpdateService.checkBzjStatus(loanapplyExpand);
									log.info("校验新老系统是否可以划拨：[{}]", urlFromId, tfr.getFlag());
									if (!tfr.getFlag()) {
										result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, tfr.getResult());
										return result;
									}
									break;
								}
							}
						}
					}else{
						loanapplyExpand.setTransferNum(loanapplyExpandDetail.getTransferNum());
						loanapplyExpand.setTransferDeposit(new BigDecimal(loanapplyExpandDetail.getTrandepositNo()));
						log.info("划拨单号:[{}],划拨金额：[{}]",loanapplyExpandDetail.getTransferNum(),new BigDecimal(loanapplyExpandDetail.getTrandepositNo()));
						TranferResult tfr = loanapplyExpandUpdateService.checkBzjStatus(loanapplyExpand);
						log.info("校验新老系统是否可以划拨：[{}]", urlFromId, tfr.getFlag());
						if (!tfr.getFlag()) {
							result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, tfr.getResult());
							return result;
						}
					}
				}
			}else{
				result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "请先保存表单");
				return result;
			}
			if (!o2oDataListService.checkCodeAttachment(loanEntity.getLoanapplyNo(), FileTypeConstants.DALIST_TYPE_BZJDATA)) {
				result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "请上传打款凭证");
				return result;
			}
		} else if (urlFromId.equals(WorkFlowConstants.DEPOSIT_FROM_ID_ZJCZ_URL)) { // 收取保证金资金查账
			log.info("收取保证金资金查账URL:[{}]", WorkFlowConstants.DEPOSIT_FROM_ID_ZJCZ_URL);
			if (loanapplyExpandDetail != null) {
				if (loanapplyExpandDetail.getPayDate() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "实际到账时间不能空");
					return result;
				}
				if (loanapplyExpandDetail.getRedDeposite() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "已交保证金不能空");
					return result;
				}
				if (loanapplyExpandDetail.getRedManger() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "已交管理费不能空");
					return result;
				}
				if (loanapplyExpandDetail.getRedRisk() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "已交补偿金不能空");
					return result;
				}
				if (loanapplyExpandDetail.getTransferNum() != null) {
					loanapplyExpand.setRedDeposite(loanapplyExpandDetail.getRedDeposite());
					loanapplyExpand.setRedManger(loanapplyExpandDetail.getRedManger());
					loanapplyExpand.setRedRisk(loanapplyExpandDetail.getRedRisk());
					loanapplyExpand.setReceiveDeposit(loanapplyExpandDetail.getReceiveDeposit());
					loanapplyExpand.setPayDate(loanapplyExpandDetail.getPayDate());
					loanapplyExpand.setPayName(loanapplyExpandDetail.getPayName());
					loanapplyExpand.setPaidDeposite(loanapplyExpandDetail.getPaidDeposite());
					loanapplyExpand.setPaidManger(loanapplyExpandDetail.getPaidManger());
					loanapplyExpand.setPaidRisk(loanapplyExpandDetail.getPaidRisk());
					loanapplyExpand.setPayRemark(loanapplyExpandDetail.getPayRemark());
					loanapplyExpand.setPayDate(loanapplyExpandDetail.getPayDate());
					loanapplyExpand.setSubmitDate(loanapplyExpandDetail.getSubmitDate());
					List<O2oPaymentDetailEntity> o2oPaymentDetailList = o2oPaymentDetailService.findBillCode(loanapplyExpand.getBillCode());
					int flag=0;
					if (null == o2oPaymentDetailList || o2oPaymentDetailList.isEmpty()) {
					if(loanapplyExpandDetail.getTransferNum().contains(",")){
						String str[]=loanapplyExpandDetail.getTransferNum().split(",");
						for (int i = 0; i < str.length; i++){
							flag++;
							loanapplyExpand.setTransferNum(str[i]);
							if(loanapplyExpandDetail.getTrandepositNo().contains(",")){
								String tranStr[]=loanapplyExpandDetail.getTrandepositNo().split(",");
								for (int j = i; j < tranStr.length;){
									loanapplyExpand.setTransferDeposit(new BigDecimal(tranStr[j]));
									log.info("修改划拨单号:[{}],划拨金额：[{}]",str[i],tranStr[j]);
									TranferResult tfr = loanapplyExpandUpdateService.updateBzjStatus(loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName(),flag);
									if (!tfr.getFlag()) {
										log.info("收取保证金划拨校验失败[{}]", urlFromId, tfr.getFlag());
										result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, tfr.getResult());
										return result;
									}
									break;
								}
							}
						}
					}else{
						loanapplyExpand.setTransferNum(loanapplyExpandDetail.getTransferNum());
						loanapplyExpand.setTransferDeposit(new BigDecimal(loanapplyExpandDetail.getTrandepositNo()));
						log.info("修改划拨单号:[{}],划拨金额：[{}]",loanapplyExpandDetail.getTransferNum(),new BigDecimal(loanapplyExpandDetail.getTrandepositNo()));
						TranferResult tfr = loanapplyExpandUpdateService.updateBzjStatus(loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName(),1);
						if (!tfr.getFlag()) {
							log.info("收取保证金划拨校验失败[{}]", urlFromId, tfr.getFlag());
							result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, tfr.getResult());
							return result;
						}
					}
					}
				} else {
					loanapplyExpand.setRedDeposite(loanapplyExpandDetail.getRedDeposite());
					loanapplyExpand.setRedManger(loanapplyExpandDetail.getRedManger());
					loanapplyExpand.setRedRisk(loanapplyExpandDetail.getRedRisk());
					loanapplyExpand.setReceiveDeposit(loanapplyExpandDetail.getReceiveDeposit());
					loanapplyExpand.setPayDate(loanapplyExpandDetail.getPayDate());
					loanapplyExpand.setPayName(loanapplyExpandDetail.getPayName());
					loanapplyExpand.setPaidDeposite(loanapplyExpandDetail.getPaidDeposite());
					loanapplyExpand.setPaidManger(loanapplyExpandDetail.getPaidManger());
					loanapplyExpand.setPaidRisk(loanapplyExpandDetail.getPaidRisk());
					loanapplyExpand.setPayRemark(loanapplyExpandDetail.getPayRemark());
					loanapplyExpand.setTransferDeposit(loanapplyExpandDetail.getTransferDeposit());
					loanapplyExpand.setTransferNum(loanapplyExpandDetail.getTransferNum());
					loanapplyExpand.setPayDate(loanapplyExpandDetail.getPayDate());
					loanapplyExpand.setSubmitDate(loanapplyExpandDetail.getSubmitDate());
					o2oPaymentDetailService.saveNewEntity(loanapplyExpand, addPayment(StatusContexts.DATA_SOURCE_NEW, StatusContexts.SUBJECT_TYPE_GLF, loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName()),
							addPayment(StatusContexts.DATA_SOURCE_NEW, StatusContexts.SUBJECT_TYPE_FXBCJ, loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName()),
							addPayment(StatusContexts.DATA_SOURCE_NEW, StatusContexts.SUBJECT_TYPE_BZJ, loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName()));
				}
			} else {
				if (loanapplyExpand.getPayDate() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "实际到账时间不能空");
					return result;
				}
				if (loanapplyExpand.getRedDeposite() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "已交保证金不能空");
					return result;
				}
				if (loanapplyExpand.getRedManger() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "已交管理费不能空");
					return result;
				}
				if (loanapplyExpand.getRedRisk() == null) {
					result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "已交补偿金不能空");
					return result;
				}
				if (loanapplyExpand.getTransferNum() != null) {
					List<O2oPaymentDetailEntity> o2oPaymentDetailList = o2oPaymentDetailService.findBillCode(loanapplyExpand.getBillCode());
					if (null == o2oPaymentDetailList || o2oPaymentDetailList.isEmpty()) {
					log.info("新老系统修改划拨金额开始");
					TranferResult tfr = loanapplyExpandUpdateService.updateBzjStatus(loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName(),1);
					if (!tfr.getFlag()) {
						log.info("收取保证金划拨校验失败[{}]", urlFromId, tfr.getFlag());
						result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, tfr.getResult());
						return result;
					}
					}
				} else {
					o2oPaymentDetailService.saveNewEntity(loanapplyExpand, addPayment(StatusContexts.DATA_SOURCE_NEW, StatusContexts.SUBJECT_TYPE_GLF, loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName()),
							addPayment(StatusContexts.DATA_SOURCE_NEW, StatusContexts.SUBJECT_TYPE_FXBCJ, loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName()),
							addPayment(StatusContexts.DATA_SOURCE_NEW, StatusContexts.SUBJECT_TYPE_BZJ, loanapplyExpand, nodeCheckParam.getAuditorId(), nodeCheckParam.getAuditorName()));
				}
			}
		}
		log.info("收取保证金、管理费流程Check成功,贷款loanapplyNo：[{}]", loanEntity.getLoanapplyNo());
		result.setModel(true);
		return result;
	}

	/**
	 * Check银行送审流程
	 * 
	 * @param result
	 * @param loanEntity
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkBankSumbit(ModelResult<Boolean> result, O2oLoanapplyEntity loanEntity, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("银行送审流程Check开始,贷款loanapplyNo：[{}]", loanEntity.getLoanapplyNo());
//		if (urlFromId.equals(WorkFlowConstants.BRT_FROM_ID_CONTRACT_URL)) {
//			log.info("信贷经理表单URL：[{}]", WorkFlowConstants.BRT_FROM_ID_CONTRACT_URL);
//			if (StringUtil.isEmpty(loanEntity.getContractFlag())) {
//				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请选择合同是否齐备");
//				return result;
//			}
//			if (!o2oDataListService.checkCodeAttachment(loanEntity.getLoanapplyNo(), FileTypeConstants.DALIST_TYPE_CONTRACT)) {
//				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请上传面签材料");
//				return result;
//			}
//			if (StatusContexts.O2O_NO.equals(loanEntity.getContractFlag())) {
//				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "合同未齐备,不能通过流程");
//				return result;
//			}
//		} else 
			if (urlFromId.equals(WorkFlowConstants.SB_FROM_ID_FK)) {
			log.info("总部贷后管理表单URL：[{}]", WorkFlowConstants.SB_FROM_ID_FK);
			if (loanEntity.getWfType().equals(StatusContexts.O2O_FK_END) || loanEntity.getWfType().equals(StatusContexts.O2O_SB_BEGIN)) {
				O2oBankLoanData loanData = loanService.queryBillCode(loanEntity.getBillCode());
				if (loanData == null) {
//					if (loanEntity.getBankType().equals(BankRate.BANK_BC)) {
//						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请等待中行返回放款信息");
//					} else {
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存表单信息");
//					}
					return result;
				}
				if (StringUtil.isEmpty(loanEntity.getPaymentTime())) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "放款日不能为空");
					return result;
				}
				if (StringUtil.isEmpty(loanEntity.getEaMount())) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "批复金额不能为空");
					return result;
				}
				if (loanData != null) {
					if (StringUtil.isEmpty(loanEntity.getEaMount())) {
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "还款期限不能为空");
						return result;
					}
				}
				List<O2oPaymentsPlan> plan = o2oPaymentsService.queryBillCode(loanEntity.getBillCode());
				if (plan.isEmpty()) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先导入还款计划");
					return result;
				}
			}
		} else if (urlFromId.equals(WorkFlowConstants.SB_FROM_ID_FK_AFTER)) {
			log.info("银行申报审查表单URL：[{}]", WorkFlowConstants.SB_FROM_ID_FK_AFTER);
			if (StringUtil.isEmpty(loanEntity.getIsCheckMqCl())) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先选择合同面签视频、合同面签照片、个人手持征信授权书及身份证照片三类电子文档是否已上传");
				return result;
			}
			
			if (StringUtil.isEmpty(loanEntity.getIsPhoneCheck())) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先选择是否已电核");
				return result;
			}
			
			if (loanEntity.getIsCheckMqCl().equals(StatusContexts.O2O_NO)) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "合同面签视频、合同面签照片、个人手持征信授权书及身份证照片三类电子文档未上传齐全，请确认上传齐备后在提交。");
				return result;
			}

			if (loanEntity.getIsPhoneCheck().equals(StatusContexts.O2O_NO)) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请确认电核后在提交。");
				return result;
			}
			
			if (StringUtil.isEmpty(loanEntity.getPhoneCheckMessage())) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "电核意见不能为空。");
				return result;
			}
			O2OSpecialInterview ishasSpecialInterview = o2OSpecialInterviewService.queryEntity(loanEntity.getLoanapplyNo());
			if (ishasSpecialInterview == null || !StatusContexts.O2O_BANK_CLO.equals(ishasSpecialInterview.getBillStatus())) {
				int count = o2oLoanapplyService.findDCLoan(loanEntity.getLoanId());
				if (count > 0) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "借款人夫妻双方在系统中有终端贷/品牌贷 代偿记录,不能通过");
					return result;
				}
			}
			
			if (BankRate.BANK_BC.equals(loanEntity.getBankType())) {
				if (!o2oDataListService.checkCodeAttachment(loanEntity.getLoanapplyNo(), FileTypeConstants.DALIST_TYPE_BC_CONTRACT)) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先上传中行申报文件");
					return result;
				}
				// 更新主表可以上传申报中行申报文件 和 是否已经批量放款：N否，Y是
				O2oLoanapplyEntity loanapply = o2oLoanapplyService.findById(nodeCheckParam.getReferId());
				loanapply.setBcFlag(StatusContexts.O2O_YES);
				loanapply.setIsBankLoanAmount(StatusContexts.O2O_YES);
				int flag = o2oLoanapplyService.updateWorkFlowNodeTranfer(loanapply);
				if (flag <= 0) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "更新贷款主表失败");
					return result;
				}
			}
			if (StringUtils.isNotBlank(loanEntity.getBankType()) && loanEntity.getBankType().equalsIgnoreCase("BC")) {
				log.info("check中行推荐函是否已经上传");
				ModelResult<BCRecommendationEntity> modelResult = bcRecommendationService.queryBCRecommendationByLoanNo(loanEntity.getLoanapplyNo());
				log.info("check中行推荐函是否已经上传查询结果：[{}]", modelResult);
				if (null == modelResult.getModel()) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "推荐函必须编辑保存一次才能通过审核");
					return result;
				}
			}
			log.info("申报通过check，贷款单号：【{}】", loanEntity.getLoanapplyNo());
			LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loanEntity.getLoanapplyNo());
			//boolean isBankBC = BankRate.BANK_BC.equals(loanEntity.getBankLoanName()) || BankRate.BANK_CCBHN.equals(loanEntity.getBankLoanName())||BankRate.BANK_SXB.equals(loanEntity.getBankLoanName());//内审银行为中国银行或中国建设银行（湖南）
			boolean isBankBC = !BankRate.BANK_GACZ.equals(loanEntity.getBankLoanName());//内审银行不等于
			//boolean isLoanType = StatusContexts.LOAN_TYPE_BK.equals(loanEntity.getLoanType());//贷款为终端贷
			if (null != loanapplyExpand && isBankBC) {//&& isLoanType
				log.info("保证金流程状态：【{}】", loanapplyExpand.getBillStatus());
				boolean newOrRun = StatusContexts.WFTYPE_STATUS_RUN.equals(loanapplyExpand.getBillStatus())
						|| StatusContexts.WFTYPE_STATUS_NEW.equals(loanapplyExpand.getBillStatus());
				AccountEntity accountEntity = o2oChargeSituationService.queryLoanAccount(loanEntity.getLoanapplyNo(), "311");
				if (null == accountEntity || null == accountEntity.getBalance() || accountEntity.getBalance().compareTo(BigDecimal.ZERO) == 0 ) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "账务系统未收到保证金，该流程不能通过");
					return result;
				}
				if (newOrRun) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "未交保证金管理费，该流程不能通过");
					return result;
				}
			}
		}
		// 流程code：开始
//		if (nodeCheckParam.getNodeCode().equals("mq03") && BankRate.BANK_SXB.equals(loanEntity.getBankLoanName())) {
//			LoanapplyExpand loanapplyExpand=loanapplyExpandService.queryLoanapplyNo(loanEntity.getLoanapplyNo());
//			if (loanapplyExpand==null) {
//				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "保证金管理费流程还，该流程不能通过");
//				return result;
//			}
//		}
			
		/**
		 * 校验借款人历史配偶的贷款单是否全部已结清
		 * 
		 */
		
			Integer num = loanapplyDubboService.isSpouseExsitNotHK(loanEntity.getLoanId(), loanEntity.getLoanapplyNo());
			if (num > 0) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "该借款人的历史配偶有未结清的贷款单");
				return result;

			}
		
		log.info("银行申报流程Check成功,贷款loanapplyNo：[{}]", loanEntity.getLoanapplyNo());
		result.setModel(true);
		return result;
	}

	/**
	 * Check归档流程
	 * 
	 * @param result
	 * @param loanEntity
	 * @param urlFrom
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkArchiveBackFile(ModelResult<Boolean> result, O2oLoanapplyEntity loanEntity, String urlFrom, WFNodeCheckParam nodeCheckParam) {
		log.info("归档流程Check开始,贷款loanapplyNo：[{}]", loanEntity.getLoanapplyNo());
		String formType=ArchiveConstants.FORMTYPE_ARCHIVE;
		String bankType = ArchiveConstants.getBankTypeId(loanEntity.getBankLoanName());
		String loanapplyNo=loanEntity.getLoanapplyNo();
		
		if (ArchiveConstants.isArchiveRequestForm(urlFrom, WorkFlowConstants.ARCHIVE_FROM_ID_HQ_URL)) {
			formType=ArchiveConstants.FORMTYPE_ARCHIVE_HQ_CHECK;
		} else if (ArchiveConstants.isArchiveRequestForm(urlFrom, WorkFlowConstants.ARCHIVE_FROM_ID_PROVINCE_URL)) {
			formType=ArchiveConstants.FORMTYPE_ARCHIVE_PROVINCE_CHECK;
		} else if (ArchiveConstants.isArchiveRequestForm(urlFrom, WorkFlowConstants.ARCHIVE_FROM_ID_URL)) {
			formType=ArchiveConstants.FORMTYPE_BANK;
		}
		com.eascs.admin.common.vo.ModelResult<ArchiveRecordEntity> modelResult = archiveRecordService.queryArchiveRecord(loanapplyNo, bankType, formType);
		ArchiveRecordEntity model = modelResult.getModel();
		if (model == null) {
			result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存档案归档表单");
			return result;
		}
		log.info("归档流程Check成功,贷款loanapplyNo：[{}]", loanEntity.getLoanapplyNo());
		result.setModel(true);
		return result;
	}
	
	/**
	 * Check合作方申请流程
	 * 
	 * @param result
	 * @param loanEntity
	 * @param urlFrom
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkPartnerBackFile(ModelResult<Boolean> result, O2oLoanapplyEntity loanEntity, String urlFrom, WFNodeCheckParam nodeCheckParam) {
//		String partenrId=nodeCheckParam.getReferId();
//		String partenrCode=nodeCheckParam.getReferCode();
//		log.info("合作方申请流程Check开始,合作方申请ID：[{}],code：[{}]", partenrId,partenrCode);
//		if ("BP_FXBSP".equals(nodeCheckParam.getNodeCode())) {
//			o2oPartnerDao.passPartnerStart(partenrId);
//		}
//		log.info("合作方申请流程Check成功,合作方申请ID：[{}],code：[{}]", partenrId,partenrCode);
//		result.setModel(true);
		return result;
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               回款check
	 *               </p>
	 * @author CXW
	 * @date 2016年12月23日 下午3:03:44
	 * @version V1.0
	 */
	private ModelResult<Boolean> checkBadLoan(ModelResult<Boolean> result, WFNodeCheckParam nodeCheckParam) {
		log.info("回款check开始,还款记录id：[{}]", nodeCheckParam.getReferId());
		O2oBadRecordEntity badRecordEntity = service.getByBadRecordID(nodeCheckParam.getReferId());
		if (nodeCheckParam.getNodeCode().equals("badLoanSckin")) {// 流程code：badLoanSckin
			log.info("回款check开始,请先保存收款信息表单.");
			if (StringUtil.isEmpty(badRecordEntity.getPayee())) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存收款信息表单");
				return result;
			}
			O2oDataListEntity datalist=fileService.getByIdAndByType(nodeCheckParam.getReferId(), FileTypeConstants.DALIST_TYPE_RPMENT);
			log.info("回款check附件,datalist={}",datalist);
			if (datalist==null) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先上传还款凭证");
				return result;
			}
		}
		if (nodeCheckParam.getNodeCode().equals("badLoanZhon")) {
			if (StringUtil.isEmpty(badRecordEntity.getThisPaymentPrincipal())) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存还款审核表单");
				return result;
			}
		}
		result.setModel(true);
		return result;
	}
	
	@Autowired
	O2oSettleService o2oSettleService;
	/**
	 * @Description:
	 *               <p>
	 *               代偿结清流程check
	 *               </p>
	 * @author CXW
	 * @date 2016年12月23日 下午3:03:44
	 * @version V1.0
	 */
	private ModelResult<Boolean> checkSettle(ModelResult<Boolean> result, WFNodeCheckParam nodeCheckParam) {
		log.info("代偿结清check开始,还款记录id：[{}]", nodeCheckParam.getReferId());
		O2oSettleVo vo = new O2oSettleVo();
		vo.setEaLoadId(nodeCheckParam.getReferId());
		O2oSettleEntity o2oSettleEntity = o2oSettleService.findEntity(vo);
		if (nodeCheckParam.getNodeCode().equals("SettleCheck")) {// 流程code：SettleCheck
			if (StringUtil.isEmpty(o2oSettleEntity.getSetlleTime())) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存结清信息表单");
				return result;
			}
		}
		result.setModel(true);
		return result;
	}
	
	@Autowired
	O2oGenerationService generationService;

	/**
	 * @Description:
	 * 				<p>
	 *               代偿结清流程check
	 *               </p>
	 * @author CXW
	 * @date 2016年12月23日 下午3:03:44
	 * @version V1.0
	 */
	private ModelResult<Boolean> checkGeneration(ModelResult<Boolean> result, WFNodeCheckParam nodeCheckParam) {
		log.info("代偿结清check开始,还款记录id：[{}]", nodeCheckParam.getReferId());
		O2oGenerationEntity generationEntity = generationService.getById(nodeCheckParam.getReferId());
		if (generationEntity != null && StringUtil.isEmpty(generationEntity.getPlayUsername())) {
			result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存代还款信息表单");
			return result;
		}
		result.setModel(true);
		return result;
	}

	/**
	 * Check付款流程
	 * 
	 * @param result
	 * @param refreId
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	@SuppressWarnings("unused")
	private ModelResult<Boolean> checkPay(ModelResult<Boolean> result, String refreId, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("付款表单check开始,贷款单号为：[{}]", refreId);
		
		//总部资金部门付款 ，需要填付款时间
		O2oPayEntity o2oPayEntity = o2oPayservice.findById(refreId);
		O2oLoanapplyEntity loanEntity = o2oLoanapplyService.findById(o2oPayEntity.getLoanapplyid());
		log.info("付款表单check开始,节点编号为======================================:", nodeCheckParam.getNodeCode());
		if(nodeCheckParam.getNodeCode()!=null){
			if(nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_FK_GBZJBMFK)){
				if(o2oPayEntity.getPayDate()==null){
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存付款时间");
					return result;
				}
			}
			if(StatusContexts.PAYMENT_TYPE_XF.equals(o2oPayEntity.getPayFkType()) && (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_FK_CWCS)  || nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_FK_GBCWFS))){
				if(o2oPayEntity.getPayDate()==null){
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存付款时间");
					return result;
				}
			}
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_FK_GBCWFS)||nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_FK_GBZJBMFK)) {
				if (StringUtils.isEmpty(o2oPayEntity.getPayFkType())) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存付款类型");
					return result;
				}
			}
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_FK_DC)) {
				if (StringUtils.isEmpty(o2oPayEntity.getLoanType())) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存代偿类型");
					return result;
				}
			}
			
			
		}
		
		if (urlFromId.equals(WorkFlowConstants.PAY_INSERT_CHECK) || urlFromId.equals(WorkFlowConstants.PAY_UPDATE_CHECK)) {
			int i = o2oPayservice.getCheckCount(nodeCheckParam.getReferId());
			log.debug("付款表单是否保存：【{}】", i);
			if (i <= 0) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存付款信息");
				return result;
			}
		}
		log.info("付款表单check成功,贷款单号为：[{}]", refreId);
		result.setModel(true);
		return result;
	}

	
	/**
	 * Check新征信动态表单
	 * 
	 * @param result
	 * @param refreId
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkNewZX(ModelResult<Boolean> result, String refreId, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("付款表单check开始,贷款单号为：[{}]", refreId);
		
		O2oZxEntity o2oZxEntity = o2oZxServiceImpl.getEntityById(refreId);
		O2oLoanUserEntityZx  o2oLoanUserEntityZx=o2oLoadUserZxServiceImpl.getEntityByZxNo(refreId);
		
		log.info("付款表单check开始,节点编号为======================================:", nodeCheckParam.getNodeCode());
		if(nodeCheckParam.getNodeCode()!=null){
					
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_NZX_ZBZXSS)) {// 总部征信送审
				if (o2oZxEntity.getZxBank().equals("BC")) {// 如果征信银行为中国银行
					if (o2oZxEntity.getPurchaseAmount() == null) {
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存“中行送审数据”模块中的企业采购金额");
						return result;
					}
					if (StringUtil.isBlank(o2oZxEntity.getJtdName())) {
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存“中行送审数据”模块中的经办平台确认");
						return result;
					}else if(o2oZxEntity.getJtdName().contains("金融部")){// 如果经办平台确认字段名称中包含“金融部”三个字
						result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "经办平台不能是省区金融部");
						return result;
					}
				}
			}
			
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_NZX_ZBZXQR)) {// 总部征信确认
				if (o2oLoanUserEntityZx.getZxDate()==null) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存征信时间");
					return result;
				}
				if (!o2oDataListService.checkCodeAttachment(refreId, FileTypeConstants.DALIST_TYPE_ZXBGDATA)) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请上传征信");
					return result;
				}
			}
			
		}
		
		log.info("付款表单check成功,贷款单号为：[{}]", refreId);
		result.setModel(true);
		return result;
	}
	
	
	/**
	 * Check企业征信动态表单
	 * 
	 * @param result
	 * @param refreId
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkQYZX(ModelResult<Boolean> result, String refreId, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("企业征信check开始,贷款单号为：[{}]", refreId);
		
		O2oEnterpriseZxEntity o2oEnterpriseZxEntity  = o2oEnterpriseZxService.getEntityById(refreId);
		
		log.info("企业征信check开始,节点编号为======================================:", nodeCheckParam.getNodeCode());
		if(nodeCheckParam.getNodeCode()!=null){
			
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.O2O_WF_CODE_QYZX_ZBZYQR)) {
				if (o2oEnterpriseZxEntity.getZxTime()==null) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存征信时间");
					return result;
				}
				if (!o2oDataListService.checkCodeAttachment(refreId, FileTypeConstants.ENTERPRISE_ZX_IDCARD)) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请上传企业征信");
					return result;
				}
			}
			
			if(nodeCheckParam.getNodeCode().equals(WorkFlowConstants.O2O_WF_CODE_QYZX_ZBZXSS1)){
				if (!o2oDataListService.checkCodeAttachment(refreId, FileTypeConstants.ENTERPRISE_ZX_PFZXZZM)) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请上传浦发银行企业征信中征码");
					return result;
				}
			}
			
			if(nodeCheckParam.getNodeCode().equals(WorkFlowConstants.O2O_WF_CODE_QYZX_TJR)){
				if (!o2oDataListService.checkCodeAttachment(refreId, FileTypeConstants.ENTERPRISE_ZX_PFZXSQS)) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "企业征信授权书（有中征码）");
					return result;
				}
			}
			
		}
		log.info("企业征信check成功,贷款单号为：[{}]", refreId);
		result.setModel(true);
		return result;
	}


	
	/**
	 * 生成明细 O2oPaymentDetailEntity对象
	 * 
	 * @param type
	 *            新老系统 old new
	 * @param code
	 *            保证金、管理费、风险补偿金
	 * @param loanapplyExpand
	 * @param auditorId
	 * @return
	 */
	private O2oPaymentDetailEntity addPayment(String type, String code, LoanapplyExpand loanapplyExpand, String auditorId, String auditorName) {
		O2oPaymentDetailEntity paymentDetil = new O2oPaymentDetailEntity();
		paymentDetil.setLoanapplyNo(loanapplyExpand.getLoanapply_no());
		if (StatusContexts.DATA_SOURCE_NEW.equals(type)) {
			paymentDetil.setBillCode(loanapplyExpand.getBillCode());
			paymentDetil.setHappenSource(loanapplyExpand.getBillCode());
			paymentDetil.setPaymentType(StatusContexts.PAYMENT_TYPE_S);
			if (StatusContexts.SUBJECT_TYPE_BZJ.equals(code)) {
				paymentDetil.setSubjectType(StatusContexts.SUBJECT_TYPE_BZJ);
				paymentDetil.setAmountMoney(loanapplyExpand.getRedDeposite());
				paymentDetil.setRemark("系统单号:" + loanapplyExpand.getBillCode() + ",收取保证金：" + loanapplyExpand.getRedDeposite());
			} else if (StatusContexts.SUBJECT_TYPE_GLF.equals(code)) {
				paymentDetil.setSubjectType(StatusContexts.SUBJECT_TYPE_GLF);
				paymentDetil.setAmountMoney(loanapplyExpand.getRedManger());
				paymentDetil.setRemark("系统单号:" + loanapplyExpand.getBillCode() + "收取管理费：" + loanapplyExpand.getRedManger());
			} else {
				paymentDetil.setSubjectType(StatusContexts.SUBJECT_TYPE_FXBCJ);
				paymentDetil.setAmountMoney(loanapplyExpand.getRedRisk());
				paymentDetil.setRemark("系统单号:" + loanapplyExpand.getBillCode() + ",收取风险补偿金：" + loanapplyExpand.getRedRisk());
			}
		} else if (StatusContexts.DATA_SOURCE_OLD.equals(type)) {
			log.info("旧系统划出单号：{}，划出金额：{}", loanapplyExpand.getTransferNum(), loanapplyExpand.getTransferDeposit());
			paymentDetil.setBillCode(loanapplyExpand.getTransferNum());
			paymentDetil.setSubjectType(StatusContexts.SUBJECT_TYPE_BZJ);
			paymentDetil.setHappenSource(loanapplyExpand.getBillCode());
			paymentDetil.setRemark("系统单号:" + loanapplyExpand.getTransferNum() + ",划出保证金：" + loanapplyExpand.getTransferDeposit() + ",划入单号：" + loanapplyExpand.getBillCode());
			paymentDetil.setPaymentType(StatusContexts.PAYMENT_TYPE_XF);
			paymentDetil.setAmountMoney(loanapplyExpand.getTransferDeposit());
		} else if (StatusContexts.BZJ_NEW_MODIFY.equals(type)) {
			paymentDetil.setBillCode(loanapplyExpand.getTransferNum());
			paymentDetil.setSubjectType(StatusContexts.SUBJECT_TYPE_BZJ);
			paymentDetil.setHappenSource(loanapplyExpand.getBillCode());
			paymentDetil.setRemark("系统单号:" + loanapplyExpand.getTransferNum() + ",划出保证金：" + loanapplyExpand.getTransferDeposit() + ",划入单号：" + loanapplyExpand.getBillCode());
			paymentDetil.setPaymentType(StatusContexts.PAYMENT_TYPE_XF);
			paymentDetil.setAmountMoney(loanapplyExpand.getTransferDeposit());
		} else {
			paymentDetil.setBillCode(loanapplyExpand.getBillCode());
			paymentDetil.setSubjectType(StatusContexts.SUBJECT_TYPE_BZJ);
			paymentDetil.setHappenSource(loanapplyExpand.getBillCode());
			paymentDetil.setRemark("系统单号:" + loanapplyExpand.getBillCode() + ",划入保证金：" + loanapplyExpand.getTransferDeposit() + ",划出单号：" + loanapplyExpand.getTransferNum());
			paymentDetil.setPaymentType(StatusContexts.PAYMENT_TYPE_XS);
			paymentDetil.setAmountMoney(loanapplyExpand.getTransferDeposit());
		}
		paymentDetil.setRecordTime(new Date());
		paymentDetil.setOperationName(auditorName);
		paymentDetil.setOperationNo(auditorId);
		paymentDetil.setUpdateTime(loanapplyExpand.getPayDate());
		return paymentDetil;
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               check退保证金流程中是否填写实际付款时间
	 *               </p>
	 * @author CXW
	 * @date 2016年11月4日 下午1:48:35
	 * @version V1.0
	 */
	@Autowired
	private O2OComBankFeeService o2OComBankFeeService;
	private ModelResult<Boolean> checkRetreat(ModelResult<Boolean> result, O2oLoanapplyEntity loanEntity, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("退费(保证金、管理费)流程表单check开始,贷款单号为：[{}],流程code={}", loanEntity.getLoanapplyNo(), nodeCheckParam.getNodeCode());
		if (urlFromId.equals(WorkFlowConstants.REFUND_MGR_FEE_URL)) {
			O2OComBackFee o2OComBackFee = o2OComBankFeeService.queryEntity(loanEntity.getLoanapplyNo(),
					StatusContexts.WFTYPE_STATUS_RUN);
			if (StringUtil.isEmpty(o2OComBackFee.getPayDate())) {
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存退费信息");
				return result;
			}
		}
		if(nodeCheckParam.getNodeCode().equals(WorkFlowConstants.REFUND_MGR_FEE_NODECODE)){
			LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loanEntity.getLoanapplyNo());
			if(null!=loanapplyExpand){
				if(StringUtil.isBlank(loanapplyExpand.getTkBankAccount())){
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存退费信息");
					return result;
				}
			}else{
				result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "未查询到保证金退费信息");
				return result;
			}
		}
		log.info("退费(保证金、管理费)流程表单check成功,贷款单号为：[{}]", loanEntity.getLoanapplyNo());
		result.setModel(true);
		return result;
	}
	
	/**
	 * @Title: checkTQHK
	 * @Description: 提前还款结清
	 * @author 张庆奇
	 * @param result
	 * @param advancedPayment
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 * @return ModelResult<Boolean>
	 * @throws
	 * @date 2017年2月21日 下午2:46:30
	 */
	private ModelResult<Boolean> checkTQHK(ModelResult<Boolean> result, AdvancedPayment advancedPayment, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		if (urlFromId.equals(WorkFlowConstants.TQHQ_FROM_ID_TQHQ_SSYH_URL)) {
			if (StringUtil.isEmpty(advancedPayment.getSettleDate())) {
				result.withError(WorkFlowConstants.TQHQ_TYPE_FAILL, "结清日期不能为空");
				return result;
			}
		}
		result.setModel(true);
		return result;
	}
	
	/**
	 * @Title: checkTSMQSB
	 * @Description: 特殊面签申报
	 */
	private ModelResult<Boolean> checkTSMQSB(ModelResult<Boolean> result, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		if (urlFromId.equals(WorkFlowConstants.TQHQ_FROM_ID_TSMQSB_URL)) {
			O2OSpecialInterview ishasSpecialInterview =  o2OSpecialInterviewService.queryEntity(nodeCheckParam.getReferId());
			if (StringUtil.isEmpty(ishasSpecialInterview.getSpecialReasons())) {
				result.withError(WorkFlowConstants.TQHQ_TYPE_FAILL, "特殊面签申报原因不能为空");
				return result;
			}
		}
		result.setModel(true);
		return result;
	}
	/**
	 * @Description:
	 *               <p>
	 *               代偿分期流程check
	 *               </p>
	 * @author CXW
	 * @date 2017年3月9日 下午2:55:03
	 * @version V1.0
	 */
	private ModelResult<Boolean> checkBack(ModelResult<Boolean> result, String refreId, WFNodeCheckParam nodeCheckParam) {
		log.info("代偿分期流程check开始,反馈编号为：[{}],节点编号为:[{}]", refreId, nodeCheckParam.getNodeCode());
		if (nodeCheckParam.getNodeCode() != null) {
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_DCFK)) {
				O2oBackEntity o2oPayEntity = o2oBackService.getEntityById(refreId);
				if (StringUtil.isEmpty(o2oPayEntity.getLoanAssure())) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先保存反馈信息");
					return result;
				}
				O2oDataListEntity datelist=fileService.getByIdAndByType(refreId, FileTypeConstants.DALIST_TYPE_HK);
				if (datelist==null) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "请先上传代偿情况反馈资料");
					return result;
				}
			}
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.WORKFLOW_NODECODE_DZFKBLG)) {
				String fileType = "'" + FileTypeConstants.DALIST_TYPE_HK + "','" + FileTypeConstants.DALIST_TYPE_FQHKHT + "','" + FileTypeConstants.DALIST_TYPE_FQQYZP + "'";
				int i = fileService.getBackDataList(refreId, fileType);
				if (i < 3) {
					result.withError(WorkFlowConstants.BRT_TYPE_FAILL, "附件上传未齐全");
					return result;
				}
			}
		}
		result.setModel(true);
		return result;
	}
   /**
	 * @Description:
	 * 归档借阅check
	 */
	private ModelResult<Boolean> checkGDBorrow(ModelResult<Boolean> result, String refreId, WFNodeCheckParam nodeCheckParam) {
		result.setModel(true);
		return result;
	}
	
	/**
	 * @Description:
	 * 补充归档check
	 */
	private ModelResult<Boolean> checkGDRepeat(ModelResult<Boolean> result, String refreId, WFNodeCheckParam nodeCheckParam) {
		ArchiveRecordRepeatEntity entity = archiveRecordDao.queryArchiveRepeatRecordById(refreId);
		if (nodeCheckParam.getNodeCode().equals("GD2_PTFXJL")) {
			if (StringUtil.isEmpty(entity.getPtSendNo())) {
				result.withError("审批失败", "请先保存快递信息表单");
				return result;
			}
		}else if (nodeCheckParam.getNodeCode().equals("GD2_SYFXJL")) {
			if (StringUtil.isEmpty(entity.getSendNo())) {
				result.withError("审批失败", "请先保存快递信息表单");
				return result;
			}
		}
		result.setModel(true);
		return result;
	}

/**
	 * @Description:
	 *               <p>
	 *               check征信归档
	 *               </p>
	 * @author lwx
	 * @date 2017年3月16日 下午1:48:35
	 * @version V1.0
	 */
	private ModelResult<Boolean> checkCreditArchive(ModelResult<Boolean> result, CreditArchiveRecordEntity creditArchiveRecordEntity, WFNodeCheckParam nodeCheckParam) {
		log.info("征信归档表单check开始,贷款单号为：[{}],发起人为类型：[{}]，发起人姓名：[{}]", creditArchiveRecordEntity.getLoanapplyNo(),creditArchiveRecordEntity.getCustomerType(),creditArchiveRecordEntity.getCustomerName());
		
		
		if (StringUtil.isEmpty(creditArchiveRecordEntity.getLoanapplyNo())) {
			result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,LoanapplyNo不存在");
			return result;
		}else if(StringUtil.isEmpty(creditArchiveRecordEntity.getBillCode())){
			result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,贷款单号不存在");
			return result;
		}else if(StringUtil.isEmpty(creditArchiveRecordEntity.getCustomerName())){
			result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,客户姓名不存在");
			return result;
		}else if(StringUtil.isEmpty(creditArchiveRecordEntity.getCustomerType())){
			result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,客户类型不存在");
			return result;
		}else if(StringUtil.isEmpty(creditArchiveRecordEntity.getIdCard())){
			result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,身份证号不存在");
			return result;
		}else if (!o2oDataListService.checkCodeAttachment(creditArchiveRecordEntity.getId(), FileTypeConstants.DALIST_TYPE_CREDITFILING)) {
			result.withError(WorkFlowConstants.DEPOSIT_TYPE_FAILL, "请上传征信归档凭证");
			return result;
		}
		
	  if(nodeCheckParam.getReferUrl()!=null&&nodeCheckParam.getReferUrl().contains(WorkFlowConstants.CREDIT_ARCHIVE_PLAT_FORM)){//平台节点check
			if (StringUtil.isEmpty(creditArchiveRecordEntity.getAuthBookCount())) {
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,授权书数据未填写");
				return result;
			}else if(StringUtil.isEmpty(creditArchiveRecordEntity.getIdCardCopies())){
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,身份证复印件数据未填写");
				return result;
			}if(StringUtil.isEmpty(creditArchiveRecordEntity.getExpressCompanyPlatform())){
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,平台快递公司数据未填写");
				return result;
			}if(StringUtil.isEmpty(creditArchiveRecordEntity.getExpressNoPlatform())){
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,平台快递单号数据未填写");
				return result;
			}if(StringUtil.isEmpty(creditArchiveRecordEntity.getReceiverPlatform())){
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程check,平台收件人数据未填写");
				return result;
			}
		}else if(nodeCheckParam.getReferUrl()!=null&&nodeCheckParam.getReferUrl().contains(WorkFlowConstants.CREDIT_ARCHIVE_PROVINCE_FORM)){//省区节点check
			 if(StringUtil.isEmpty(creditArchiveRecordEntity.getExpressCompanyProvince())){
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程中,省区快递公司数据未填写");
				return result;
			}

			if(StringUtil.isEmpty(creditArchiveRecordEntity.getExpressNoProvince())){
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程中,省区快递单号数据未填写");
				return result;
			}

			if(StringUtil.isEmpty(creditArchiveRecordEntity.getReceiverProvince())){
				result.withError(WorkFlowConstants.CREDIT_ARCHIVE_CHECK_FAILL, "征信归档流程中,省区收件人数据未填写");
				return result;
			}
		}
		log.info("征信归档表单check成功,贷款单号为：[{}],发起人为类型：[{}]，发起人姓名：[{}]", creditArchiveRecordEntity.getLoanapplyNo(),creditArchiveRecordEntity.getCustomerType(),creditArchiveRecordEntity.getCustomerName());
		result.setModel(true);
		return result;
	}

	
	/**
	 * Check个人退款动态表单
	 * 
	 * @param result
	 * @param refreId
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkTX(ModelResult<Boolean> result, String refreId, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("个人退款check开始,贷款单号为：[{}]", refreId);
		O2oBackFeesEntity  o2oBackFees = o2oBackFeesService.queryById(refreId);
		
		log.info("个人退款check开始,节点编号为======================================:", nodeCheckParam.getNodeCode());
		if(nodeCheckParam.getNodeCode()!=null){
			
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.O2O_WF_CODE_TX_ZBZYQR)) {
				if (o2oBackFees.getBackSjNo()==null) {
					result.withError(WorkFlowConstants.TX_TYPE_FAILL, "请先保存实际退款银行账户");
					return result;
				}
				if (o2oBackFees.getBackDate()==null) {
					result.withError(WorkFlowConstants.TX_TYPE_FAILL, "请先保存实际退款时间");
					return result;
				}
			}
		}
		log.info("个人退款check成功,贷款单号为：[{}]", refreId);
		result.setModel(true);
		return result;
	}
	
	
	/**
	 * @Title: checkCollectFees
	 * @Description: 检查打款动态表单内容是否填写
	 * @author 张庆奇
	 * @param result
	 * @param refreId
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 * @return ModelResult<Boolean>
	 * @throws
	 * @date 2017年12月7日 上午11:34:12
	 */
	private ModelResult<Boolean> checkCollectFees(ModelResult<Boolean> result, String refreId, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("个人打款check开始,贷款单号为：[{}]", refreId);
		O2oCollectFee o2oCollectFee = o2oCollectFeesService.queryById(refreId);
		
		log.info("个人打款check开始,节点编号为======================================:", nodeCheckParam.getNodeCode());
		if(nodeCheckParam.getNodeCode()!=null){
			
			if (nodeCheckParam.getNodeCode().equals(WorkFlowConstants.O2O_WF_CODE_DK_ZBCWCS)) {
				if (o2oCollectFee.getCollectDate()==null) {
					result.withError(WorkFlowConstants.DK_TYPE_FAILL, "到账时间不能为空");
					return result;
				}
				if (o2oCollectFee.getCollectNo()==null) {
					result.withError(WorkFlowConstants.DK_TYPE_FAILL, "请选择收款账户");
					return result;
				}
				
				if(nodeCheckParam.getAction() == 5){
					if(StringUtils.equalsIgnoreCase(o2oCollectFee.getNoYes(), "N")){
						return result.withError(WorkFlowConstants.DK_TYPE_FAILL, "如单据打款金额与实际到账金额不一致，请驳回流程");
					}
				}
			}
		}
		log.info("个人打款check成功,贷款单号为：[{}]", refreId);
		result.setModel(true);
		return result;
	}
	
	/**
	 * Check个人退款动态表单
	 * 
	 * @param result
	 * @param refreId
	 * @param urlFromId
	 * @param nodeCheckParam
	 * @return
	 */
	private ModelResult<Boolean> checkNewCacle(ModelResult<Boolean> result, String refreId, String urlFromId, WFNodeCheckParam nodeCheckParam) {
		log.info("手动作废check开始,作废单号为：[{}]", refreId);
		O2oCacleFlowEntity o2oCacleFlowEntity = o2oCacleFlowServiceImpl.getEntityById(refreId);
		O2oLoanapplyEntity loan = null;
		
		log.info("手动作废check开始,节点编号为======================================:", nodeCheckParam.getNodeCode());
		if(nodeCheckParam.getNodeCode()!=null){
			if(o2oCacleFlowEntity!=null){
				 loan = o2oLoanapplyService.findById(o2oCacleFlowEntity.getLoanapplyNo());
			}else{
				result.withError("CACLE_0001", "找不到作废单据");
			}
			if(loan!=null){
				if(StatusContexts.O2O_YES.equals(loan.getLoanFlg())){
					result.withError("CACLE_0003", "已放款无法作废");
				}
				if(StatusContexts.O2O_SB_BEGIN.equals(loan.getWfType())){
					result.withError("CACLE_0004", "面前申报中作废");
				}
			}else{
				result.withError("CACLE_0002", "找不到借款单据");
			}
		}
		log.info("个人退款check成功,贷款单号为：[{}]", refreId);
		result.setModel(true);
		return result;
	}
	

}
